'''
Created on Oct 24, 2009

@author: kage
'''

class FileQueue():
    def __init__(self):
        self.__queue = {}
        self.__numItems = 0
        self.__urls = []
        
    def __addItem(self, item):
        if(item.inputUrl not in self.__urls):
            self.__queue[self.__numItems] = item
            self.__urls.append(item.inputUrl)
            self.__numItems += 1
            return 'added'
        else:
            return 'exists'
        
    def __removeItem(self, id):
        if(id in self.__queue.keys()):
            try:
                self.__urls.remove(self.__queue[id].inputUrl)
            except:
                print 'skipping'
            self.__queue.pop(id)
            return 'removed'
        else:
            return 'doesnt exist'
    
    def addItems(self, items):
        exists = 0
        for item in items:
            if (self.__addItem(item) == 'exists'):
                exists += 1
        
        if(exists == 0):
            return 'added'
        else:
            return 'ignored adding ' + str(exists) + ' items'
    
    def removeItems(self, ids):
        ignored = 0
        for id in ids:
            if(self.__removeItem(id) == 'doesnt exist'):
                ignored += 1
        
        if(ignored == 0):
            return 'removed'
        else:
            return 'ignored removing ' + str(ignored) + ' items'
    
    def removeAll(self):
        self.removeItems(self.__queue.keys())
    
    def sortedQueue(self):
        self.__sortedKeys = self.__queue.keys()
        self.__sortedKeys.sort()
        
        self.__sortedVals = []
        
        for key in self.__sortedKeys:
            self.__sortedVals.append(self.__queue[key])
            
        return self.__sortedVals
    
    def length(self):
        return len(self.__queue)
    
    def getItem(self, id):
        return self.__queue[id]
    
    def updateQueue(self, data):
        for key in data.keys():
            self.__queue[key] = data[key]
            
    def updateItem(self, key, value):
        self.__queue[key] = value
        
    def vals(self):
        sortedKeys = self.__queue.keys()
        sortedKeys.sort()
        return sortedKeys
        
        
    
